package com.ny.zmb.glxt.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ny.zmb.glxt.entity.PmStore;
import com.ny.zmb.glxt.entity.PmStoreExt;
import com.ny.zmb.glxt.mapper.PmStoreExtMapper;
import com.ny.zmb.glxt.mapper.PmStoreMapper;
import com.ny.zmb.glxt.service.IPmStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ny.zmb.openapi.controller.entity.Result;
import com.ny.zmb.openapi.entity.PmRegisterExamine;
import com.ny.zmb.openapi.entity.PmUser;
import com.ny.zmb.openapi.mapper.PmRegisterExamineMapper;
import com.ny.zmb.openapi.mapper.PmUserMapper;
import com.ny.zmb.openapi.service.IPmUserService;
import org.apache.ibatis.annotations.One;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;


/**
 * <p>
 * 店铺 服务实现类
 * </p>
 *
 * @author xie
 * @since 2024-10-10
 */
@Service
public class PmStoreServiceImpl extends ServiceImpl<PmStoreMapper, PmStore> implements IPmStoreService {

    @Resource
    private PmUserMapper pmUserMapper;
    @Resource
    private PmStoreMapper pmStoreMapper;
    @Resource
    private PmStoreExtMapper pmStoreExtMapper;
    @Resource
    private IPmUserService pmUserService;
    @Resource
    private PmRegisterExamineMapper pmRegisterExamineMapper;

    QueryWrapper<PmUser> pmUserQueryWrapper = new QueryWrapper<>();
    QueryWrapper<PmStore> pmStoreQueryWrapper = new QueryWrapper<>();
    QueryWrapper<PmStoreExt> pmStoreExtQueryWrapper = new QueryWrapper<>();

    @Override
    public PmStore getInfoByUserId(Integer userId) {
        LambdaQueryWrapper<PmStore> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PmStore::getUserId, userId);
        return getOne(wrapper);
    }

    @Override
    public PmStore getInfoByBusinessNo(String businessNo) {
        LambdaQueryWrapper<PmStore> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PmStore::getBusinessNo, businessNo);
        return getOne(wrapper);
    }



    @Override
    public Result commitInfoToZMB(JSONObject obj) {

        PmStore pmStore = new PmStore();
        // 转换
        pmStore = pmStore.typeChange(obj);
        if (pmStoreMapper.insertOrUpdate(pmStore)) {
            pmStoreQueryWrapper.eq("out_merchant_no", pmStore.getOutMerchantNo());
            pmStore = pmStoreMapper.selectOne(pmStoreQueryWrapper);
            pmStoreExtQueryWrapper = new QueryWrapper<>();
            pmStoreExtQueryWrapper.eq("out_merchant_no", pmStore.getOutMerchantNo());
            PmStoreExt pmStoreExt = pmStoreExtMapper.selectOne(pmStoreExtQueryWrapper);
            pmStoreExt = pmStoreExt == null ? new PmStoreExt() : pmStoreExt;
            // 捆绑关系
            pmStoreExt.setStoreId(pmStore.getId());
            pmStoreExtMapper.insertOrUpdate(pmStoreExt);
            JSONObject result = new JSONObject();
            result.put("pmStore", pmStore);
            result.put("pmStoreExt", pmStoreExt);
            return Result.ok();
        } else {
            return Result.fail("操作失败");
        }

    }

    @Override
    public PmStore getInfoByOutMerchantNo(String outMerchantNo) {
        pmStoreQueryWrapper = new QueryWrapper<>();
        pmStoreQueryWrapper.eq("out_merchant_no", outMerchantNo);
        return pmStoreMapper.selectOne(pmStoreQueryWrapper);
    }

    @Override
    public Result commitInfoToZMBByOutMerchantNo(JSONObject jsonObject, Integer userId) {
        String outMerchantNo = jsonObject.getString("outMerchantNo");
        // ======================== 校验用户信息
        pmStoreQueryWrapper = new QueryWrapper<>();
        pmStoreQueryWrapper.eq("user_id", userId);
        PmStore pmStoreUser = pmStoreMapper.selectOne(pmStoreQueryWrapper);
        // 查询结果不为空 且商户号不同
        if (pmStoreUser != null && !outMerchantNo.equals(pmStoreUser.getOutMerchantNo())) {
            throw new RuntimeException("账号信息错误,您已经申请了外部商户号,商户号为:" + pmStoreUser.getOutMerchantNo());
        }
        // ======================== 校验商户号信息
        pmStoreQueryWrapper = new QueryWrapper<>();
        pmStoreQueryWrapper.eq("out_merchant_no", outMerchantNo);
        // 查看外部商户号是否被使用
        PmStore pmStore = pmStoreMapper.selectOne(pmStoreQueryWrapper);
        if (pmStore == null) {
            // 查询推荐人
            pmUserQueryWrapper = new QueryWrapper<>();
            pmUserQueryWrapper.eq("id", userId);
            PmUser pmUser = pmUserMapper.selectOne(pmUserQueryWrapper);
            if (pmUser == null) {
                throw new RuntimeException("用户信息异常");
            }
            // 新增
            pmStore = new PmStore();
            // 推荐人ID赋值
            pmStore.setPid(pmUserService.getAllPid(pmUser));
        } else {
            // 校验
            if (!userId.equals(pmStore.getUserId())) {
                throw new RuntimeException("账号信息错误,该商户号已被其他账号使用");
            }
        }
        pmStore = pmStore.typeChange(jsonObject);
        pmStoreMapper.insertOrUpdate(pmStore);
        return Result.ok(pmStore);
    }

    @Override
    public Boolean updateMerchantNoByOutMerchantNo(String outMerchantNo, String merchantNo) {
        PmStore pmStore = getInfoByOutMerchantNo(outMerchantNo);
        if (pmStore != null) {
            pmStore.setBusinessNo(merchantNo);
            return updateById(pmStore);
        } else {
            return false;
        }
    }

    @Override
    public List<PmStore> get(PmStore obj) {
        pmStoreQueryWrapper = new QueryWrapper<>();
        if (obj != null) {
            if (obj.getOutMerchantNo() != null) pmStoreQueryWrapper.eq("out_merchant_no", obj.getOutMerchantNo());
            if (obj.getId() != null) pmStoreQueryWrapper.eq("id", obj.getId());

        }
        pmStoreMapper.selectList(pmStoreQueryWrapper);
        return Collections.emptyList();
    }


    public PmStore getStoreByBusinessNo(String businessNo) {
        pmStoreQueryWrapper = new QueryWrapper<>();
        pmStoreQueryWrapper.eq("businessNo", businessNo);
        return pmStoreMapper.selectOne(pmStoreQueryWrapper);
    }
}
